<?php

namespace app\admin\controller;

use think\Db;

class Seller
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
        $edit = request()->request('edit');
        if ($edit == 'flag_sort') {
            $id = (int)request()->request('id');
            $sort = (int)request()->request('sort');
            Db::table('seller')->where('id', $id)->update(['flag_sort' => $sort]);
        } else {
            $aid = input('get.aid');
            $phone = input('get.phone');
            $where = [];
            if (!empty($aid)) {
                $where['aid'] = $aid;
            }
            if (!empty($phone)) {
                $where['phone'] = $phone;
            }
            $pages = pages('seller', $where, input('get.page'));
            $data = Db::table("seller")->whereIn('aid', session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
            foreach ($data as $k => $v) {
                $data[$k]['cate'] = Db::table('seller_category')->where('id', $v['cate_id'])->value('title');
            }
            return view('', ['data' => $data, 'pages' => $pages['pages']]);
        }
    }

    public function map(){
		if(input('get.type')=="range"){
			return view('map_range');
		}
		return view('');
	}
	
    public function edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = input("post.");
			if(!$data['aid']){
                echo '请选择城市';
            }else{
				$data['cid'] = Db::table('area')->where(['id' => $data['aid']])->value("father_id");
				$data['pid'] = Db::table('city')->where(['id' => $data['cid']])->value("father_id");
				if (Db::table("seller")->where(['phone' => $data['phone']])->where('id', '<>', $id)->find()) {
					return error(lang('tip')['re_phone']);
				}
				vendor('dadaps.Dadaps#Api');
				$dadaApi = new \DadapsApi;
				if ($id) {
					$params = $data;
					$params['city_name'] = Db::table('city')->where(['id' => $data['cid']])->value("name");
					$params['area_name'] = Db::table('area')->where(['id' => $data['aid']])->value("name");
					$result = $dadaApi->shopUpdate($params);
					if($result['status'] == 1){
						// var_dump($result);exit;
						if (empty($data['password'])) {
							unset($data['password']);
						} else {
							$data['password'] = md5($data['password']);
						}
						Db::table("seller")->where(['id' => $id])->update($data);
					}else{
						var_dump($result);exit;
					}
				} else {
					$params = $data;
					$params['city_name'] = Db::table('city')->where(['id' => $data['cid']])->value("name");
					$params['area_name'] = Db::table('area')->where(['id' => $data['aid']])->value("name");
					$result = $dadaApi->shopAdd($params);
					if($result['status'] == 1){
						$data['dada_shop_id'] = $result['shop_id'];
						$data['regtime'] = time();
						$data['password'] = md5($data['password']);
						$id = Db::table("seller")->insertGetId($data);
					}else{
						var_dump($result);exit;
					}
				}
			}
        }
        $data = Db::table("seller")->where(['id' => $id])->find();
        return view('', ['data' => $data]);
    }

    public function del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $page = input("get.page");
        Db::table("seller")->whereIn('aid', session('aid'))->delete($id);
        Db::table("service")->where('uid', $id)->delete();
        Db::table("volume")->where('uid', $id)->delete();
        redirect(url('/admin/seller') . '?page=' . $page);
        exit;
    }

    public function upload()
    {
        $config = Db::table('material_config')->where('id', 1)->find();
        config([
            'UPLOAD_SITEIMG_QINIU' => [
                'maxSize' => 5 * 1024 * 1024,//文件大小
                'rootPath' => './',
                'saveName' => ['uniqid', ''],
                'driver' => 'Qiniu',
                'driverConfig' => [
                    'secretKey' => $config['secretKey'],
                    'accessKey' => $config['accessKey'],
                    'domain' => $config['domain'],
                    'bucket' => $config['bucket'],
                ],
            ],
            'file_size' => $config['file_size']
        ]);
        $setting = config('UPLOAD_SITEIMG_QINIU');
        $Upload = new \think\Upload($setting);
        $infos = $Upload->upload($_FILES);
        return $infos;
    }

    public function category()
    {
        $aid = request()->request('aid');
        $where = [];
        if ($aid) {
            $where['aid'] = $aid;
        }
        $data = Db::table("seller_category")->whereIn('aid', session('aid'))->where($where)->order('id asc')->select();
        return view('', ['data' => $data]);
    }

    public function category_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
			$data = request()->post();
			if(!$data['aid']){
				echo '请选择城市';
			}else{
				if ($_FILES['image']['tmp_name'] != '') {
					$image = $this->upload();
					$data['image'] = $image['image']['url'];
				}
				if (!empty($id)) {
					Db::table("seller_category")->whereIn('aid', session('aid'))->where('id', $id)->update($data);
				} else {
					Db::table("seller_category")->insert($data);
				}
				echo 'please wait...';
				return '<script>parent.location.reload();</script>';
			}
        } else {
            $data = [];
            if (!empty($id)) {
                $data = Db::table('seller_category')->whereIn('aid', session('aid'))->where('id', $id)->find();
            }
            error_reporting(0);
            return view('', ['data' => $data]);
        }
    }

    public function category_del()
    {
        $id = (int)request()->request('id');
        Db::table("seller_category")->whereIn('aid', session('aid'))->where('id', $id)->delete();
        $return = array(
            'code' => 'success',
            'msg' => lang('currency')['success']
        );
        return $return;
    }

    public function get_seller_category()
    {
        $aid = request()->request('aid');
        $where = [];
        if ($aid) {
            $where['aid'] = $aid;
        }
        $data = Db::table('seller_category')->whereIn('aid', session('aid'))->where($where)->select();
        $return = array(
            'data' => $data
        );
        return $return;
    }

    public function setting()
    {
        if (request()->isPost()) {
            $data = input("post.");
            Db::table("seller_config")->where(['id' => 1])->update($data);
        }
        $data = Db::table("seller_config")->where(['id' => 1])->find();
        return view('', ['data' => $data]);
    }

    public function money_off()
    {
        $seller_id = request()->request('id');
        $data = Db::table('money_off')->where(['seller_id' => $seller_id])->select();
        return view('', ['data' => $data]);
    }

    public function money_off_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = input("post.");
            if((($data['system_minus']+$data['seller_minus']) > $data['minus']) || (empty($data['system_minus'])) && empty($data['seller_minus'])){
                echo '错误的配置！';
            }else{
                if ($id) {
                    Db::table("money_off")->where(['id' => $id])->update($data);
                } else {
                    $id = Db::table("money_off")->insertGetId($data);
                }
            }
        }
        $data = Db::table("money_off")->where(['id' => $id])->find();
        return view('', ['data' => $data]);
    }

    public function money_off_del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $seller_id = input("get.seller_id") ? input("get.seller_id") : 0;
        $page = input("get.page");
        Db::table("money_off")->delete($id);
        redirect(url('seller/money_off') . '?page=' . $page.'&id='.$seller_id);
        exit;
    }

    public function desk()
    {
        $seller_id = request()->request('id');
        $data = Db::table('desk')->where(['seller_id' => $seller_id])->select();
        return view('', ['data' => $data]);
    }

    public function desk_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = input("post.");
            if ($id) {
				Db::table("desk")->where(['id' => $id])->update($data);
			} else {
				$data['code'] = md5($data['title'].time().rand(0,99).rand(0,99).rand(0,99));
				$id = Db::table("desk")->insertGetId($data);
			}
        }
        $data = Db::table("desk")->where(['id' => $id])->find();
        return view('', ['data' => $data]);
    }

    public function desk_del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $seller_id = input("get.seller_id") ? input("get.seller_id") : 0;
        $page = input("get.page");
        Db::table("desk")->delete($id);
        redirect(url('seller/desk') . '?page=' . $page.'&id='.$seller_id);
        exit;
    }

}
